//
//  ApplicationListViewController.m
//  MobileOffice
//
//  Created by Appel on 16/10/11.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "ApplicationListViewController.h"
#import "AddressBookViewController.h"
#import "StatisticsListViewController.h"
#import "ManageStrutureModel.h"
#import "PersonInfoDetailViewController.h"

#define getManageStructureList @"getManageStructureList"//组织机构筛选

@interface ApplicationListViewController ()

@property (nonatomic,strong) ManageStrutureModel *manageModel;
@end

@implementation ApplicationListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftBackNavigation];
    [self setNavigationTitle:@"应用"];
    [self setRightBackNavigation];
    [self creatView];
    // Do any additional setup after loading the view.
}

- (void)creatView{
    //蓝色条形
    UILabel *label=[MyControl creatLabelWithFrame:CGRectMake(8, 8, 8, 35) text:@""];
    label.backgroundColor=COLOR_SKYBLUE;
    label.layer.masksToBounds=YES;
    label.layer.cornerRadius=label.width/2;
    [self.view addSubview:label];
    
    //常用软件
    UILabel *title=[MyControl creatLabelWithFrame:CGRectMake(20, 15, 80, 21) text:@"常用软件"];
    [self.view addSubview:title];
    
    //管理
    UILabel *manage=[MyControl creatLabelWithFrame:CGRectMake(IPHONE_WIDTH-48, title.y, 40, 21) text:@"管理"];
    [self.view addSubview:manage];
    //button间隔
    CGFloat buttonWidth=IPHONE_WIDTH/3;
    CGFloat buttonHeight=buttonWidth/3*2;
    //设置边条
    for (int i=0; i<3; i++) {
        UILabel *line1=[[UILabel alloc]initWithFrame:CGRectMake(0, (title.y+title.height+15)+buttonHeight*i, IPHONE_WIDTH, 1)];
        line1.backgroundColor=BACKCOLOR_GRAY;
        //line1.backgroundColor=[UIColor lightGrayColor];
        [self.view addSubview:line1];
        
        if (i<2) {
            UILabel *line2=[[UILabel alloc]initWithFrame:CGRectMake(buttonWidth*(i+1),(title.y+title.height+15) , 1, 2*buttonHeight)];
            line2.backgroundColor=BACKCOLOR_GRAY;
            //line2.backgroundColor=[UIColor lightGrayColor];
            [self.view addSubview:line2];
        }
    }
    
    //添加button
    NSArray *nameArr=@[@"员工动态",@"统计"];
    NSArray *imageArr=@[@"k_ic_txl2@2x",@"k_ic_tj2@2x"];
    for (NSInteger i=0; i<nameArr.count; i++) {
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake((buttonWidth-60)/2+buttonWidth*i, (title.y+title.height+15)+(buttonHeight-80)/2, 60, 80);
        button.tag=110+i;
        //button.backgroundColor=[UIColor redColor];
        button.titleLabel.font=[UIFont systemFontOfSize:13];
        [button setTitle:nameArr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleEdgeInsets=UIEdgeInsetsMake(50, -60, 10, 0);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        button.imageEdgeInsets=UIEdgeInsetsMake(10, 10, 30, 10);
        [button setImage:[UIImage imageNamed:imageArr[i]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
//        UILabel *title=[MyControl creatLabelWithFrame:CGRectMake(0, 60, button.width, 20) text:nameArr[i]];
//        title.textAlignment=NSTextAlignmentCenter;
//        [button addSubview:title];
    }
}

- (void)buttonClick:(UIButton *)button{
    switch (button.tag) {
        case 110://通讯录
        {
            AddressBookViewController *address=[AddressBookViewController new];
            [self.navigationController pushViewController:address animated:YES];
        }
            break;
        case 111://统计
        {
            [self getManageStructureListRequest];
//            PersonInfoDetailViewController *person=[PersonInfoDetailViewController new];
//            [self.navigationController pushViewController:person animated:YES];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -获取筛选信息
- (void)getManageStructureListRequest{
    NSDictionary *dict=@{
                         };
    
    //   字典转json
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString* str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //拼接soap字符串
    NSString* soap = [NSString stringWithFormat:@"<sessionId>%@</sessionId>\n<content>%@</content>",[LZXHelper getSessionID],str];
    // __weak AddressBookViewController *mySelf=self;
    [LZXHelper postLoadViewWithText:HUD_PLEASEWAIT View:[[UIApplication sharedApplication].delegate window]];
    [HttpRequest SOAPData:[LZXHelper getServerURL] SoapMthod:getManageStructureList soapBody:soap success:^(id responseObject) {
        [LZXHelper hideLoadViewWithView:[[UIApplication sharedApplication].delegate window]];
        
        //解析数据
        self.manageModel=[[ManageStrutureModel alloc]initWithResponse:responseObject];
        if ([self.manageModel.code isEqualToString:@"200"]) {
            //如果有值就跳转到部门
            if (self.manageModel.dataArr.count>0) {

                StatisticsListViewController *address=[StatisticsListViewController new];
                address.manageModel=self.manageModel;
                [self.navigationController pushViewController:address animated:YES];
            }else{
                //如果数组返回为空 则直接跳转到详情
                PersonInfoDetailViewController *person=[PersonInfoDetailViewController new];
                [self.navigationController pushViewController:person animated:YES];
            }
        }else if([self.manageModel.code isEqualToString:@"220"]){
            [LZXHelper postMessageWithText:self.manageModel.msg View:[[UIApplication sharedApplication].delegate window]];
        }
    } failure:^(NSError *error) {
        [LZXHelper hideLoadViewWithView:[[UIApplication sharedApplication].delegate window]];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
